#!/bin/bash
# scripts/deploy.sh

set -e

# 配置变量
REGISTRY_URL="localhost:5000"
IMAGE_NAME="gd-data-platform"
VERSION=${1:-latest}
FULL_IMAGE_NAME="${REGISTRY_URL}/${IMAGE_NAME}:${VERSION}"

echo "开始部署应用..."
echo "镜像名称: ${FULL_IMAGE_NAME}"

# 检查镜像是否存在
if ! docker image inspect ${FULL_IMAGE_NAME} > /dev/null 2>&1; then
    echo "镜像不存在，正在拉取..."
    docker pull ${FULL_IMAGE_NAME}
fi

# 停止现有容器
echo "停止现有容器..."
docker-compose down || true

# 更新 docker-compose.yml 中的镜像版本
echo "更新镜像版本..."
sed -i "s|image: .*|image: ${FULL_IMAGE_NAME}|g" docker-compose.yml

# 启动服务
echo "启动服务..."
docker-compose up -d

# 等待服务启动
echo "等待服务启动..."
sleep 30

# 健康检查
echo "执行健康检查..."
./scripts/health-check.sh

echo "部署完成！"
echo "访问地址: http://localhost"
